<?php
class zb extends CI_Controller {
	function index() {
		$this -> pub -> logp('浏览账本', '1-2-3');
		$da = $this -> pub -> get_setting('账本', 'ZB');

		if ($this -> session -> userdata('state') == 3) {
			$this -> db -> where('staff_id', $this -> session -> userdata('sid'));
		}

		$q = $this -> db -> get_where('sp', array('xffs' => 1, 'sp_del' => 1));

		$da['sp'] = $q -> result_array();

		$this -> load -> view('adminx/zb', $da);
	}

	//获取账本
	function get_wx() {
		if ($this -> session -> userdata('state') == 3) {
			$this -> db -> where('staff_id', $this -> session -> userdata('sid'));
		} else {
			if ($this -> input -> post('staff_id')) {
				$this -> db -> where('staff_id', $this -> input -> post('staff_id'));
			}
		}

		$div = '<tr><td>';
		$q = $this -> db -> get_where('staff');
		$staff = $q -> row_array(0);

		$this -> db -> select('sum(je)');
		$q = $this -> db -> get_where('jb', array('mid' => $staff['mid'], 'state' => 1, 'type' => -1));
		$jb = $q -> row_array(0);
		$div .= $jb['sum(je)'];
		$div .= '</td><td>';

		$this -> db -> select('sum(je)');
		$q = $this -> db -> get_where('jb', array('mid' => $staff['mid'], 'state' => 1, 'type' => 5));
		$jb = $q -> row_array(0);
		$div .= $jb['sum(je)'];

		$div .= '</td><td>';

		$this -> db -> select('sum(je)');
		$q = $this -> db -> get_where('jb', array('mid' => $staff['mid'], 'state' => 2, 'type' => -1));
		$jb = $q -> row_array(0);
		$div .= $jb['sum(je)'];

		$div .= '</td><td>';

		$div .= $jb;
		$div .= '</td></tr>';

		echo $div;
	}

	function add() {
		$this -> pub -> logp('创建账本', '1-2');
		$da['staff_id'] = $this -> input -> post('staff_id');
		$da['je'] = $this -> input -> post('je');
		$da['date'] = $this -> input -> post('date');
		$da['title'] = $this -> input -> post('title');
		$da['bz'] = $this -> input -> post('bz');
		$da['type'] = $this -> input -> post('type');
		$da['zb_del'] = 1;
		$da['mktime'] = mktime();
		$this -> pub -> insert('zb', $da);
		echo json_encode(array('type' => 1));
	}

	function up() {
		$this -> pub -> logp('账本修改 - ' . $this -> input -> post('idx'), '1-2');
		$da['staff_id'] = $this -> input -> post('staff_id');
		$da['je'] = $this -> input -> post('je');
		$da['date'] = $this -> input -> post('date');
		$da['title'] = $this -> input -> post('title');
		$da['type'] = $this -> input -> post('type');
		$da['bz'] = $this -> input -> post('bz');
		$this -> db -> update('zb', $da, array('id' => $this -> input -> post('idx')));
		echo json_encode(array('type' => 1));
	}

	function get_one() {
		$this -> pub -> logp('获取账本信息 - ' . $this -> input -> post('idx'), '1-2-3');
		$q = $this -> db -> get_where('zb', array('id' => $this -> input -> post('idx')));
		if ($q -> num_rows() > 0) {
			$da = $q -> row_array(0);
			$da['typex'] = 1;
			echo json_encode($da);
		} else {
			echo json_encode(array('typex' => 2));
		}
	}

	function del() {
		$this -> pub -> logp('账本删除 - ' . $this -> input -> post('idx'), '1-2-3');
		$da['type'] = 1;
		$this -> db -> update('zb', array('zb_del' => 2), array('id' => $this -> input -> post('idx')));
		echo json_encode($da);
	}

	function get() {
		$this -> pub -> logp('获取账本信息', '1-2-3');
		$this -> db -> where('zb_del', 1);
		if ($this -> input -> post('staff_id') && $this -> session -> userdata('state') != 3) {
			$this -> db -> where('staff_id', $this -> input -> post('staff_id'));
		}

		if ($this -> session -> userdata('state') == 3) {
			$this -> db -> where('staff_id', $this -> session -> userdata('sid'));
		}

		$q = $this -> db -> get_where('zb');
		$da['content'] = '';
		foreach ($q->result_array() as $v) {
			$this -> db -> select('name');
			$qq = $this -> db -> get_where('staff', array('staff_id' => $v['staff_id']));
			$staff = $qq -> row_array(0);
			$da['content'] .= '<tr>';
			$da['content'] .= '<td>' . $staff['name'] . '</td>';
			$da['content'] .= '<td>' . $v['title'] . '</td>';
			$da['content'] .= '<td>' . $v['date'] . '</td>';
			$da['content'] .= '<td>' . $v['je'] . '</td>';
			$da['content'] .= '<td>' . ($v['type'] == 1 ? '收入' : '支出') . '</td>';
			$da['content'] .= '<td>' . $v['bz'] . '</td><td>';
			if ($this -> session -> userdata('state') != 3) {
				$da['content'] .= '<button onclick="show_up(\'' . $v['id'] . '\')" type="button"  class="btn default"><i class="fa fa-edit"></i>修改</button>  <button onclick="del(\'' . $v['id'] . '\',\'' . $v['zb'] . '\')" type="button" class="btn red"><i class="fa fa-trash-o"></i> 删除</button>';
			}
			$da['content'] .= '</td></tr>';
		}
		$da['type'] = 1;
		echo json_encode($da);
	}

}
?>